const Router = require('koa-router')
//

const {
  create,
  update,
  get,
  list,
  del,
  login,
  updatePwd,
} = require('../controller/sys_user')

const {
  createValidator,
  updateValidator,
  delValidator,
  listValidator,
  loginValidator,
  updatePwdValidator,
} = require('../middleware/validator/sys_user')

// validateToken, validateAdmin 有先后顺序 先校验token再校验管理员
const { validateToken, validateAdmin } = require('../middleware/auth/token')
// 密码解密
const { passwdDecryptSm4 } = require('../middleware/decrypt/passwd')

const PREFIX = '/v1/sys_user'

const router = new Router({ prefix: PREFIX })

router.get('/get', validateToken, get)

router.put('/update', validateToken, updateValidator, update)

router.post('/login', loginValidator, passwdDecryptSm4, login)

router.put(
  '/updatePwd',
  validateToken,
  updatePwdValidator,
  passwdDecryptSm4,
  updatePwd
)

// 后端管理员权限
router.post(
  '/create',
  validateToken,
  validateAdmin,
  createValidator,
  passwdDecryptSm4,
  create
)

// 后端管理员权限
router.del('/del', validateToken, validateAdmin, delValidator, del)

// 后端管理员权限
router.get('/list', validateToken, validateAdmin, listValidator, list)

module.exports = router
